home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
utils
/
unix
/
unzip-5.12
/
extract.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-15
|
47KB
|
1,134 lines
/*---------------------------------------------------------------------------
extract.c
This file contains the high-level routines ("driver routines") for extrac-
ting and testing zipfile members. It calls the low-level routines in files
explode.c, inflate.c, unreduce.c and unshrink.c.
---------------------------------------------------------------------------*/
#include "unzip.h"
#include "crypt.h"
#ifdef MSWIN
# include "wizunzip.h"
# include "replace.h"
#endif
int newfile; /* used also in file_io.c (flush()) */
ulg *crc_32_tab; /* used also in file_io.c and crypt.c (full version) */
static void makecrc __((void));
static int store_info __((void));
static int extract_or_test_member __((void));
/*******************************/
/* Strings used in extract.c */
/*******************************/
static char Far VersionMsg[] =
" skipping: %-22s need %s compat. v%u.%u (can do v%u.%u)\n";
static char Far ComprMsg[] =
" skipping: %-22s compression method %d\n";
static char Far FilNamMsg[] =
"%s: bad filename length (%s)\n";
static char Far ExtFieldMsg[] =
"%s: bad extra field length (%s)\n";
static char Far OffsetMsg[] =
"file #%d: bad zipfile offset (%s): %ld\n";
static char Far ExtractMsg[] =
"%8sing: %-22s %s%s";
#ifndef SFX
static char Far LengthMsg[] =
"%s %s: %ld bytes required to uncompress to %lu bytes;\n %s\
supposed to require %lu bytes%s%s%s\n";
#endif
static char Far BadFileCommLength[] = "\n%s: bad file comment length\n";
static char Far LocalHdrSig[] = "local header sig";
static char Far BadLocalHdr[] = "\nfile #%d: bad local header\n";
static char Far AttemptRecompensate[] = " (attempting to re-compensate)\n";
static char Far SkipVolumeLabel[] = " skipping: %-22s %svolume label\n";
static char Far ReplaceQuery[] =
"replace %s? [y]es, [n]o, [A]ll, [N]one, [r]ename: ";
static char Far AssumeNone[] = " NULL\n(assuming [N]one)\n";
static char Far NewName[] = "new name: ";
static char Far InvalidResponse[] = "error: invalid response [%c]\n";
static char Far ErrorInArchive[] = "At least one %serror was detected in %s.\n";
static char Far ZeroFilesTested[] = "Caution: zero files tested in %s.\n";
#ifndef VMS
static char Far VMSFormat[] =
"\n%s: stored in VMS format. Extract anyway? (y/n) ";
#endif
#ifdef CRYPT
static char Far SkipCantGetPasswd[] =
" skipping: %-22s unable to get password\n";
static char Far SkipIncorrectPasswd[] =
" skipping: %-22s incorrect password\n";
static char Far FilesSkipBadPasswd[] =
"%d file%s skipped because of incorrect password.\n";
static char Far MaybeBadPasswd[] =
" (may instead be incorrect password)\n";
#else
static char Far SkipEncrypted[] =
" skipping: %-22s encrypted (not supported)\n";
#endif
static char Far NoErrInCompData[] =
"No errors detected in compressed data of %s.\n";
static char Far NoErrInTestedFiles[] =
"No errors detected in %s for the %d file%s tested.\n";
static char Far FilesSkipped[] =
"%d file%s skipped because of unsupported compression or encoding.\n";
static char Far ErrUnzipFile[] = " error: %s%s %s\n";
static char Far ErrUnzipNoFile[] = "\n error: %s%s\n";
static char Far NotEnoughMem[] = "not enough memory to ";
static char Far InvalidComprData[] = "invalid compressed data to ";
static char Far Inflate[] = "inflate";
#ifndef SFX
static char Far Explode[] = "explode";
static char Far Unshrink[] = "unshrink";
#endif
static char Far FileUnknownCompMethod[] = "%s: unknown compression method\n";
static char Far BadCRC[] = " bad CRC %08lx (should be %08lx)\n";
static char Far UnsupportedExtraField[] =
"warning: unsupported extra field compression type--skipping\n";
static char Far BadExtraFieldCRC[] =
"error [%s]: bad extra field CRC %08lx (should be %08lx)\n";
/**************************************/
/* Function extract_or_test_files() */
/**************************************/
int extract_or_test_files() /* return PK-type error code */
{
uch *cd_inptr;
int cd_incnt, error, error_in_archive=PK_COOL;
int i, j, renamed, query, len, filnum=(-1), blknum=0;
int *fn_matched=NULL, *xn_matched=NULL;
ush members_remaining, num_skipped=0, num_bad_pwd=0;
long cd_bufstart, bufstart, inbuf_offset, request;
LONGINT old_extra_bytes=0L;
static min_info info[DIR_BLKSIZ];
/*---------------------------------------------------------------------------
The basic idea of this function is as follows. Since the central di-
rectory lies at the end of the zipfile and the member files lie at the
beginning or middle or wherever, it is not very desirable to simply
read a central directory entry, jump to the member and extract it, and
then jump back to the central directory. In the case of a large zipfile
this would lead to a whole lot of disk-grinding, especially if each mem-
ber file is small. Instead, we read from the central directory the per-
tinent information for a block of files, then go extract/test the whole
block. Thus this routine contains two small(er) loops within a very
large outer loop: the first of the small ones reads a block of files
from the central directory; the second extracts or tests each file; and
the outer one loops over blocks. There's some file-pointer positioning
stuff in between, but that's about it. Btw, it's because of this jump-
ing around that we can afford to be lenient if an error occurs in one of
the member files: we should still be able to go find the other members,
since we know the offset of each from the beginning of the zipfile.
---------------------------------------------------------------------------*/
pInfo = info;
members_remaining = ecrec.total_entries_central_dir;
#if (defined(CRYPT) || !defined(NO_ZIPINFO))
newzip = TRUE;
#endif
/* malloc space for CRC table and generate it */
if ((crc_32_tab = (ulg *)malloc(256*sizeof(ulg))) == (ulg *)NULL)
return PK_MEM2;
makecrc();
/* malloc space for check on unmatched filespecs (OK if one or both NULL) */
if (filespecs > 0 &&
(fn_matched=(int *)malloc(filespecs*sizeof(int))) != (int *)NULL)
for (i = 0; i < filespecs; ++i)
fn_matched[i] = FALSE;
if (xfilespecs > 0 &&
(xn_matched=(int *)malloc(xfilespecs*sizeof(int))) != (int *)NULL)
for (i = 0; i < xfilespecs; ++i)
xn_matched[i] = FALSE;
/*---------------------------------------------------------------------------
Begin main loop over blocks of member files. We know the entire central
directory is on this disk: we would not have any of this information un-
less the end-of-central-directory record was on this disk, and we would
not have gotten to this routine unless this is also the disk on which
the central directory starts. In practice, this had better be the ONLY
disk in the archive, but maybe someday we'll add multi-disk support.
---------------------------------------------------------------------------*/
while (members_remaining) {
j = 0;
/*
* Loop through files in central directory, storing offsets, file
* attributes, case-conversion and text-conversion flags until block
* size is reached.
*/
while (members_remaining && (j < DIR_BLKSIZ)) {
--members_remaining;
pInfo = &info[j];
if (readbuf(sig, 4) == 0) {
error_in_archive = PK_EOF;
members_remaining = 0; /* ...so no more left to do */
break;
}
if (strncmp(sig, central_hdr_sig, 4)) { /* just to make sure */
FPRINTF(stderr, LoadFarString(CentSigMsg), j); /* sig not found */
FPRINTF(stderr, LoadFarString(ReportMsg)); /* check binary transfers */
error_in_archive = PK_BADERR;
members_remaining = 0; /* ...so no more left to do */
break;
}
/* process_cdir_file_hdr() sets pInfo->hostnum, pInfo->lcflag */